#!/usr/bin/env python

import os
import urllib
import json

from google.appengine.api import users
from google.appengine.ext import ndb

import jinja2
import webapp2

from baseClasses import BaseRequestHandler
import datetime

JINJA_ENVIRONMENT = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'markup')),
    extensions=['jinja2.ext.autoescape'],
    autoescape=True)
    
    
class Index(BaseRequestHandler):
    def get(self, *args, **kwargs):
        template = JINJA_ENVIRONMENT.get_template('main_index.html')
        #self.response.set_cookie('zx-project-radar-auth', 'YAHOO', expires=datetime.datetime.now(), path='/', domain='zx-project-radar.appspot.com')
        #self.response.set_cookie('zx', 'YAHOO', expires=datetime.datetime.now(), path='/')
        self.response.set_cookie('zx', 'YAHOO', max_age=360, path='/')
        self.response.write(template.render(self.template_values))

class StaticPageHandler(BaseRequestHandler):
    def get(self, *args, **kwargs):
        if len(args) > 0:
            template = JINJA_ENVIRONMENT.get_template("about_%s.html" %  args[0].replace("-", "_"))
            self.response.write(template.render(self.template_values))
        else:
            self.response.write('Page not found.')
        
class RedirectionHandler(BaseRequestHandler):
    def get(self, *args, **kwargs):
        if (len(args) > 0):
            self.redirect("/%s/" % args)
        

config = {}
config['webapp2_extras.sessions'] = {
    'secret_key': 'some-secret-key',
}
#app = webapp2.WSGIApplication([
#    ('/', MainHandler)
#], debug=True)

#1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
#---------!---------!---------!---------!---------!---------!---------!---------!---------!---------!
#http://stackoverflow.com/questions/3343566/url-with-trailing-slash-in-google-app-engine
"""ZX: 
    It is recommended that the routing urls have (?i) to make it case-insensitive.
    Routing urls should end with slash (/).
    If there's no slash, browser (eg Google Chrome) might treat it as a document 
    and perform search instead of routing it.
"""
app = webapp2.WSGIApplication([
    ('/', Index),
    (r'(?i)^/about/(project-goals)$', StaticPageHandler),
    (r'(?i)^/about/(development-stack)$', StaticPageHandler)
    #(r'(?i)^/projects/api/(\w+(?:-\w+)*)$', ProjectsApiHandler),
    #(r'(?i)^/projects/(\w+)*$', ProjectsPageHandler),
    #(r'(?i)^/json/(\w+(?:-\w+)*)$', JsonPageHandler),
    #(r'(?i)^/(\w+)$', StaticPageHandler)
    #(r'(?i)^/machines/(\w+)?/?$', MachinesPageHandler),
    #(r'(?i)^/master/os/$', OperatingSystemPageHandler),
    ##(r'(?i)^/applications/(\d+?)?$', ApplicationsPageHandler),
    #(r'(?i)^/resources/(/\d+?)?$', ResourcesPageHandler),
    #(r'(?i)^/api/machines/$', MachineService)
], debug=True, config=config)